Skip to content

fix(context-window): prevent context-limit chat crashes with recovery UX, warnings, and context compaction#46

Merged
DJJones66 merged 1 commit intomainfrom
fix/context-window-overflow-recovery
Apr 9, 2026
Merged

fix(context-window): prevent context-limit chat crashes with recovery UX, warnings, and context compaction#46
DJJones66 merged 1 commit intomainfrom
fix/context-window-overflow-recovery

Conversation

@DJJones66
Copy link
Copy Markdown
Collaborator

Summary

This PR resolves the launch-blocking context window crash by implementing a 3-phase fix across Gateway and Web client:

  • Phase 0: Human-readable, actionable context-overflow messaging and recovery actions
  • Phase 1: Near-limit warning surfaced to users before hard failure
  • Phase 2: Proactive Gateway-side context management with deterministic compaction/summarization

What changed

Phase 0 (immediate user recovery)

  • Replaced technical overflow text with user-safe actionable copy in:
    • builds/typescript/engine/errors.ts
    • builds/typescript/engine/loop.ts
  • Added overflow-specific chat actions:
    • Start New Conversation
    • Continue in New Conversation
  • Suppressed irrelevant Open Settings route for context-overflow errors

Phase 1 (near-limit prevention)

  • Added Gateway context warning metadata on /message response headers
  • Parsed warning metadata in client adapter/hook
  • Rendered “session getting long” warning indicator in chat UI

Phase 2 (proactive context management)

  • Added Gateway context-window manager:
    • Token estimation for prompt + tool schemas
    • Deterministic replay-unit budgeting
    • Preservation of assistant tool-call + tool-result continuity
    • Automatic compaction/summarization of older turns
    • Summary artifact persistence under memory conversations

Files touched (high level)

  • Gateway:
    • builds/typescript/gateway/context-window.ts (new)
    • builds/typescript/gateway/server.ts
  • Engine:
    • builds/typescript/engine/errors.ts
    • builds/typescript/engine/loop.ts
  • Web client:
    • builds/typescript/client_web/src/api/gateway-adapter.ts
    • builds/typescript/client_web/src/api/useGatewayChat.ts
    • builds/typescript/client_web/src/components/chat/ChatPanel.tsx
    • builds/typescript/client_web/src/components/chat/ErrorMessage.tsx
    • related type and test files

Tests

Added/updated tests for:

  • Context overflow error classification and messaging
  • Gateway context-window compaction and summary artifact behavior
  • Header-driven near-limit warning propagation
  • Hook/UI overflow-specific action paths and warning rendering

Validation run

  • Backend:
    • npm run build
    • npm test -- engine/errors.test.ts gateway/context-window.test.ts
  • Web:
    • npm run typecheck
    • npm test -- src/api/gateway-adapter.test.ts src/api/useGatewayChat.test.tsx src/components/chat/ChatPanel.test.tsx

All passed locally.

Architecture alignment

No contract drift introduced:

  • Gateway->Engine handoff remains messages + metadata
  • Context budgeting remains Gateway-owned
  • Engine stream event taxonomy unchanged
  • No request-metadata config side channel introduced

@DJJones66 DJJones66 merged commit d310c4e into main Apr 9, 2026
@davewaring davewaring deleted the fix/context-window-overflow-recovery branch April 13, 2026 23:38
davewaring added a commit that referenced this pull request Apr 14, 2026
Cherry-picked doc files from docs/d112-developer-docs branch.
Code changes from that branch excluded — they conflict with PRs #46-#51.
These docs need a review pass to align with current codebase state.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant